#pragma once

#include "NetworkingCommon.h"

uint32 ReadUInt32(void * addr, int offset) {
#ifdef PLATFORM_LITTLE_ENDIAN
	const uint8 converted[4] = {
						*((uint8*)addr + offset + 3),
						*((uint8*)addr + offset + 2),
						*((uint8*)addr + offset + 1),
						*((uint8*)addr + offset)
	};
	return *((uint32 *) converted);
#else
	return *((uint32 *) addr);
#endif
}

uint16 ReadUInt16(void * addr, int offset) {
#ifdef PLATFORM_LITTLE_ENDIAN
	const uint8 converted[2] = {
						*((uint8*)addr + offset + 1),
						*((uint8*)addr + offset)
	};
	return *((uint16 *)converted);
#else
	return *((uint16 *)addr);
#endif
}

// TODO: not checked whether it's working well or not!! 
void WriteUInt32(void * addr, int offset, uint32 value) {
#ifdef PLATFORM_LITTLE_ENDIAN
	*((uint8*)addr + offset + 3) = (uint8) (value & 0xFF);
	*((uint8*)addr + offset + 2) = (uint8)((value >> 8) & 0xFF);
	*((uint8*)addr + offset + 1) = (uint8)((value >> 16) & 0xFF);
	*((uint8*)addr + offset) = (uint8)((value >> 24) & 0xFF);
#else
	*((uint32*)addr) = value;
#endif
}

// TODO: not checked whether it's working well or not!! 
void WriteUInt16(void * addr, int offset, uint16 value) {
#ifdef PLATFORM_LITTLE_ENDIAN
	*((uint8*)addr + offset + 1) = (uint8)(value & 0xFF);
	*((uint8*)addr + offset) = (uint8)((value >> 8) & 0xFF);
#else
	*((uint16*)addr) = value;
#endif
}

